<template>
  <div class="app-container">
    <!-- 表单 -->
    <el-dialog :close-on-click-modal="false" :before-close="handlerCancel" :visible.sync="visible" title="修改储位数量" width="450px">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="140px">
        <el-form-item label="储位类型" prop="storageType">
          <el-select v-model="form.storageType" style="width: 230px;">
            <el-option
              v-for="item in dict.storage_type"
              :key="item.id"
              :label="item.label"
              :value="Number(item.value)"
              :disabled="Number(item.value) !== form.storageType"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="上限库存数量" prop="upperLimitStorageQty">
          <el-input v-model="form.upperLimitStorageQty" readonly style="width: 230px;" />
        </el-form-item>
        <el-form-item label="上限储位比例" prop="upperLimitScale">
          <el-input v-model="form.upperLimitScale" readonly style="width: 230px;" />
        </el-form-item>
        <el-form-item label="已用储位数量" prop="usedStorageQty">
          <el-input v-model="form.usedStorageQty" readonly style="width: 230px;" />
        </el-form-item>
        <el-form-item label="剩余可用空储位数量" prop="freeStorageQty">
          <el-input v-model="form.freeStorageQty" readonly style="width: 230px;" />
        </el-form-item>
        <el-form-item :label="(flag === 'add' ? '新增' : '减少') + '储位数量'" prop="adjustQuantity">
          <el-input v-model="form.adjustQuantity" style="width: 230px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="handlerCancel">取消</el-button>
        <el-button :loading="btn.edit" type="primary" @click="handlerSubmit">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table ref="table" v-loading="loading" :data="list" :border="false" size="small" style="width: 100%;">
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column prop="storageType" label="储位类型">
        <template slot-scope="scope">
          {{ dict.label.storage_type[scope.row.storageType] }}
        </template>
      </el-table-column>
      <el-table-column prop="upperLimitStorageQty" label="上限库存数量" />
      <el-table-column prop="upperLimitScale" label="上限储位比例">
        <template slot-scope="scope">
          {{ scope.row.upperLimitScale.toFixed(2) * 100 }}%
        </template>
      </el-table-column>
      <el-table-column prop="usedStorageQty" label="已用储位数量" />
      <el-table-column prop="freeStorageQty" label="剩余可用空储位数量" />
      <el-table-column label="操作" width="250px" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-plus" @click="addStorage(scope.row)">新增数量</el-button>
          <el-button type="success" icon="el-icon-minus" @click="reduceStorage(scope.row)">减少数量</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getStoragePercentAPI, adjustAPI } from '@/api/work/storageQtyManagement'
export default {
  name: 'StorageQtyManagement',
  dicts: ['storage_type'],
  data() {
    return {
      list: [],
      loading: false,
      visible: false,
      rules: {},
      form: {},
      btn: {
        edit: false
      },
      flag: ''
    }
  },
  mounted() {
    this.toQuery()
  },
  methods: {
    async toQuery() {
      try {
        this.loading = true
        const res = await getStoragePercentAPI()
        if (res.result === 'OK') {
          this.list = res.data
        }
      } finally {
        this.loading = false
      }
    },
    addStorage(row) {
      this.flag = 'add'
      const jsonString = JSON.stringify(row)
      this.form = JSON.parse(jsonString)
      this.visible = true
    },
    reduceStorage(row) {
      this.flag = 'reduce'
      const jsonString = JSON.stringify(row)
      this.form = JSON.parse(jsonString)
      this.visible = true
    },
    handlerCancel() {
      this.form = {}
      this.visible = false
      this.flag = ''
    },
    async handlerSubmit() {
      const submitForm = {
        storageType: this.form.storageType,
        freeStorageQty: this.form.freeStorageQty
      }
      if (this.flag === 'add') {
        submitForm.addQty = this.form.adjustQuantity
        submitForm.isAddOrReduce = 1
      } else {
        submitForm.reduceQty = this.form.adjustQuantity
        submitForm.isAddOrReduce = 2
      }
      try {
        this.btn.edit = true
        const res = await adjustAPI(submitForm)
        if (res.result === 'OK') {
          this.$message.success('修改成功')
          this.toQuery()
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.edit = false
        this.visible = false
      }
    }
  }
}
</script>

<style scoped>

</style>
